package com.aisino.cvasp.application.controller;

import com.aisino.cvasp.application.domain.Education;
import com.aisino.cvasp.application.domain.FormData;
import com.aisino.cvasp.application.service.CreateBarcode;
import com.aisino.cvasp.application.service.CreateForm;
import com.aisino.cvasp.application.service.CreatePhoto;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class FormTestController {


    public  static String  PDF_NEW_PATH = "D:\\privateWorkSpace\\my-itext5-pdf-master\\my-itext5-pdf-master\\src\\main\\resources\\template\\new.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        // TODO:   1 安装破解版 acrobat dc
        // TODO:   2 制作模板，及表单域
        // TODO:   3 读取模板

        org.springframework.core.io.Resource resource = new ClassPathResource("template/template.pdf");
        InputStream inputStream = resource.getInputStream();


        FileOutputStream fos = new FileOutputStream(PDF_NEW_PATH);

        // TODO:   4 读取模板表单
        PdfReader reader = new PdfReader(inputStream);
        PdfStamper ps = new PdfStamper(reader, fos);
        // 设置最大化 压缩
        ps.setFullCompression();

        //TODO:   5 获取pdf表单
        AcroFields acroFields = ps.getAcroFields();
        // 设置表单内容 全局字体
        //为指定表单 设置 居中,换行等 通过 Acrobat 工具设置
        BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        BaseFont bf2 = BaseFont.createFont("/tff/malgun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //韩文
        BaseFont bf3 = BaseFont.createFont("/tff/times.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //欧洲
        BaseFont bf4 = BaseFont.createFont("/tff/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
       // BaseFont bf5 = BaseFont.createFont("/tff/msgothic.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //日文

        acroFields.addSubstitutionFont(bf);
        acroFields.addSubstitutionFont(bf2);
        acroFields.addSubstitutionFont(bf3);
        acroFields.addSubstitutionFont(bf4);
        
        // TODO:   6 开始绘制表单数据
        CreateForm.createForm( initData(),acroFields, ps);
        // TODO:   7 开始绘制图片
        CreatePhoto.createPhoto(acroFields,ps);
        // TODO:   8 开始绘制条形码
        CreateBarcode.CreateBarcode(acroFields, ps);
        // TODO: 9 新增一空白页
        ps.insertPage(3, PageSize.A4);

        // 禁止文本域再次编辑
        ps.setFormFlattening(true);

        // TODO:   9输出保存
        ps.flush();
        ps.close();
        reader.close();
        inputStream.close();


    }
    public static FormData initData(){
        FormData formData = new FormData();
        formData.setCenterName("青岛石老人雕像园");
        formData.setApplicantName("徐 康");
        formData.setGender("F");
        formData.setPiBirthCity("周边交通方便，购物生活也方便，有一个中青建安公司在楼上青岛前几名的建筑公司，还有市北城发公司。配套设施完善，负1楼食堂干净卫生，菜品丰富，服务也很棒，办公");

        Education education = new Education();
        education.setEdBeginTime(new Date());
        education.setEdEndTime(new Date());
        education.setEdSchoolName("青岛五十八中");
        education.setEdSchoolAddress("李沧九水东路");
        Education education2 = new Education();
        education2.setEdBeginTime(new Date());
        education2.setEdEndTime(new Date());
        education2.setEdSchoolName("青岛理工大学");
        education2.setEdSchoolAddress("山东路");
        List<Education> list = Arrays.asList(education,education2);
        formData.setEducation(list);


        return formData;
    }




}
